use reqwest::{Client, ClientBuilder};
use std::sync::{Arc, OnceLock};
// use std::sync::LazyLock;
use std::time::Duration;
use tokio::sync::RwLock;
// static STK_XCHG_MAP: LazyLock<HashMap<&str, &str>> =
//     LazyLock::new(|| HashMap::from([("SSE", "上交所"), ("SZSE", "深交所"), ("BSE", "北交所")]));

pub const CONFIG_FILE: &'static str = "config.toml";

pub const DATE_FORMAT: &str = "%Y%m%d";
pub const APP_PORT_CFG: &'static str = "app.port";
pub const APP_ENV_CFG: &'static str = "app.env";
pub const APP_LOG_PATH_CFG: &'static str = "log.path";
pub const APP_LOG_NAME_CFG: &'static str = "log.filename";

pub const TU_SHARE_HOST: &'static str = "http://api.tushare.pro";
pub const TU_SHARE_TOKEN_CFG: &'static str = "tushare.tokens";

//const是编译期的常量，使用到这个常量的地方实际是编译成AtomicUsize::new(0)
//static是运行时全局变量，就这么一个实例
// pub static  TU_SHARE_TOKEN_IDX: AtomicUsize = AtomicUsize::new(0);

static TU_SHARE_TOKEN_IDX: OnceLock<Arc<RwLock<usize>>> = OnceLock::new();

pub fn get_tushare_idx() -> &'static Arc<RwLock<usize>> {
    TU_SHARE_TOKEN_IDX.get_or_init(|| Arc::new(RwLock::new(0)))
}

pub const API_STOCK_DAILY: &'static str = "daily";
pub const API_STOCK_COMP: &'static str = "stock_company";

pub const GPT_API_HOST_CFG: &'static str = "gpt.api_host";
pub const GPT_API_KEY_CFG: &'static str = "gpt.api_key";

pub const WECOM_TOKEN_CFG: &'static str = "wecom.token";
pub const WECOM_AES_KEY_CFG: &'static str = "wecom.encoding_aes_key";
pub const WECOM_APP_ID_CFG: &'static str = "wecom.app_id";
pub const WECOM_CORP_ID_CFG: &'static str = "wecom.corp_id";
pub const WECOM_AGENT_ID_CFG: &'static str = "wecom.app_agent_id";
pub const WECOM_APP_SECRET_CFG: &'static str = "wecom.app_secret";
pub const WECOM_ACCESS_TOKEN_URL_CFG: &'static str = "wecom.access_token_url";
pub const WECOM_MESSAGE_PUSH_URL_CFG: &'static str = "wecom.message_push_url";

pub fn http_cli() -> Client {
    ClientBuilder::new()
        .danger_accept_invalid_certs(true)
        // 设置连接池
        // 每个主机保持的最大空闲连接数
        .pool_max_idle_per_host(5)
        // 空闲10分钟连接失效
        .pool_idle_timeout(Duration::from_secs(600))
        // TCP 配置
        // TCP keepalive
        .tcp_keepalive(Duration::from_secs(1800))
        // 超时设置
        // 连接超时
        .connect_timeout(Duration::from_secs(10))
        // 请求超时
        .timeout(Duration::from_secs(20))
        // https相关配置；这里不是只用https协议，所以关掉
        .https_only(false)
        .use_rustls_tls()
        .build()
        .unwrap_or_else(|e| panic!("cannot build reqwest client: {}", e))
}
